// component/pay-way/pay-way.js
const config = require('../../config.js');
const request = require('../../utils/request.js')
const utils = require('../../utils/util.js');
let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
      amount:String
  },

  /**
   * 组件的初始数据
   */
  data: {
      curStatus:1
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _itemClick(e){
        console.log(e)
        let dataSet = e.currentTarget.dataset;
        if (dataSet.type == 'wechat'){
            this.setData({
                curStatus:1 
            })
        }else{
            this.setData({
                curStatus: 2
            })
        }
        this.triggerEvent('payItemSelect', {
            type: dataSet.type
        });
    },
    //支付
    _orderPayWay(params,cb){
        console.log(params)
        let that = this;
        if(params.pay_type=='amount'){
            this._amountPay(params)
        }else if(params.pay_type == 'wechat'){
            this._amountPay(params,function(data){
                let options = data;
                options.openid = app.globalData.memberInfo.js_openid
                //模拟1分钱支付
                //data.pay_price = 0.01;
                that._getWxPayParams(data,cb)
            })
        }
    },
    //获取微信支付参数
      _getWxPayParams(data, cb){
        let that = this
        wx.showLoading({
            title: '加载中...',
        })
        request.ajaxRequest({
            url: '/wap/api/?task=PayPayWechatjspay',
            params: data,
            successCb: function (data) {
                wx.hideLoading();
                console.log(data)
                that.wxPayInterface(data, cb)

            },
            errorCb: function (data) {
                console.log(data)
                wx.showToast({
                    icon: "none",
                    title: data.message,
                })
            }
        })
    },
    //调用微信支付接口
    wxPayInterface(data,cb){
        if(data.type == 'zero'){
            wx.showToast({
                    icon:"none",
                    title: '支付成功',
                })
                setTimeout(function(){
                    if(typeof cb == 'function'){
                        cb()
                    }
                    
                },500)
                reutrn;
        }
        wx.requestPayment({
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            'success': function (res) {
                console.log(res);
                wx.showToast({
                    icon:"none",
                    title: '支付成功',
                })
                setTimeout(function(){
                    if(typeof cb == 'function'){
                        cb()
                    }
                    
                },500)
               
            },
            'fail': function (res) {
                if (res.errMsg == '"requestPayment:fail cancel"'){
                    wx.showToast({
                        icon: "none",
                        title: '操作取消',
                    })
                }
            }
        })
    },
    //余额支付和微信支付切换
    _amountPay(params,successCb){
        let that = this
        wx.showLoading({
            title: '加载中...',
        })
        request.ajaxRequest({
            url: '/wap/api/?task=OrderPayPaycart',
            params: params,
            successCb: function (data) {
                wx.hideLoading();
                if (data.type == "score") {
                    wx.showToast({
                        icon: "none",
                        title: '支付成功',
                    })
                    setTimeout(function () {
                        wx.redirectTo({
                            url: "/pages/appoint-result/appoint-result?type=2",
                        })

                    }, 500)
                    reutrn;
                }
                if(params.pay_type == 'amount'){
                    wx.redirectTo({
                        url: "/pages/appoint-result/appoint-result?type=2",
                    })
                }else{
                    if (typeof successCb == 'function'){
                        successCb(data)
                    }
                }
                

            },
            errorCb: function (data) {
                console.log(data)
                wx.showToast({
                    icon: "none",
                    title: data.message,
                })
            }
        })
    }
  }
})
